<template>
  <div class="main-box">
    <InterfaceCat></InterfaceCat>
    <div class="table-box">
      <InterfaceTabs></InterfaceTabs>

      <div class="table-container" v-show="showInterfaceList">
        <InterfaceList></InterfaceList>
      </div>
      <!--      <InterfaceList ></InterfaceList>-->


      <Interface v-for="interfaceItem in interfaceSourceList"
                 v-show="!showInterfaceList && interfaceItem.id ===currentActiveTab"
                 @sendRequest="sendRequest"
                 @saveRequest="saveRequest"
                 :currentInterface="interfaceItem"></Interface>
    </div>

  </div>
</template>


<script setup>
import {computed, onMounted, onUnmounted, ref} from "vue";
import InterfaceCat from '@/views/interface/interfaceManage/components/interfaceCat/index.vue'
import InterfaceTabs from "@/views/interface/interfaceManage/components/interfaceTabs/index.vue";
import InterfaceList from '@/views/interface/interfaceManage/components/interfaceList/index.vue'
import Interface from "@/views/interface/interfaceManage/components/interface/index.vue";

import {useInterfaceStore} from "@/stores/modules/interface/interface.js";
import {storeToRefs} from "pinia";

const interfaceStore = useInterfaceStore()

const {
  showInterfaceList,
  interfaceSourceList,
  currentActiveTab,
} = storeToRefs(interfaceStore)

const sendRequest = (currentInterface)=>{
  interfaceStore.sendRequest(currentInterface)
}

const saveRequest = (currentInterface)=>{
  interfaceStore.saveRequest(currentInterface)
}

onMounted(()=>{
  $eventBus.on("RequestReturnResult",returnResult=>{

    console.log("请求接口已返回")
    let result = JSON.parse(returnResult)
    interfaceSourceList.value.forEach(item=>{
      if (item.id === result["interface_case_id"]){
        item["response_id"] = result["response_id"]
        item["response_time"] = result["response_time"]
        item["response_status"] = result["response_status"]
        item["response_size"] = result["response_size"]
        item["response_headers"] = JSON.stringify(JSON.parse(result["response_headers"]),null,4)
        item["response_body"] = result["response_body"]
        item["console_out"] = result["console_out"]
      }
    })
    console.log(interfaceSourceList.value)
  })
})

onUnmounted(()=>{
  $eventBus.off("RequestReturnResult")
})


</script>


